package BanyuanClub.Study.Day0303;

import java.util.zip.ZipInputStream;

public class MyLinkedList {
    public Node first;
    public Node list;
    public int size;

    //增头
    public void addFirst(Object obj){
        Node temp = new Node();
        temp.date = obj;
        //当前链表为空时
        if (size == 0){
            this.first = temp;
            this.list = temp;
            size++;
            return;
        }
        temp.nest = this.first;
        this.first.per = temp;
        this.first = temp;
        size++;
    }

    //增尾
    public void addList(Object obj){
        Node temp = new Node();
        temp.date = obj;
        //当前链表为空时
        if (size == 0){
            this.first = temp;
            this.list = temp;
            size++;
            return;
        }
        //旧尾节点的下一个指向新节点
        this.list.nest = temp;
        //新节点的上一个指向旧尾节点
        temp.per = this.list;
        //新的尾节点是新节点
        this.list = temp;
        size++;
    }

    //插队
    public void addCenter(int index , Object obj){
        Node newNode = new Node();
        newNode.date = obj;
        if (index < 0 || index > size){
            return;
        }
        Node temp = null;
        if (index < size/2){
            temp = first;
            for (int i = 0; i < index; i++) {
                temp = temp.nest;
            }
        }else {
            temp = list;
            for (int i = size; i > index; i--) {
                temp = temp.per;
            }

        }
        //保存本节点的上一个节点
        Node theNode = temp.per;
        temp.per = newNode;
        newNode.nest = temp;
        theNode.nest = newNode;
        newNode.per = theNode;
    }

    //掐头
    public void removeFirst(){
        if (first == null){
            throw new IllegalStateException();
        }
        //新节点的下一个为旧头节点的下一个
        Node temp = first.nest;
        //旧头节点为空
        first.nest = null;
        first.date = null;
        //新头节点为新节点
        first = temp;
    }

    //去尾
    public void removeList(){
        this.first.date = null;
        this.first.nest.per = null;
        this.first = this.first.nest;
    }

    //查询
    public Object get(int index){
        if (index < 0 || index >= size){
            return null;
        }
        if (size == 0){
            return null;
        }
        Node temp = new Node();
        if (index < size/2){
            temp = first;
            for (int i = 0; i < index; i++) {
                temp = temp.nest;
            }
            return temp.date;
        }else {
            temp = list;
            for (int i = size; i > index; i--) {
                temp = temp.per;
            }
            return temp.date;
        }

    }

    //size方法
    public int size(){
        return size;
    }

    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();

    }
}


class Node{
    public Object date;
    public Node nest;
    public Node per;
}